<?php

class Konfiguracja extends Zend_Db_Table {

    protected $_name = 'konfiguracja';

    public function getAnalytics($id = 0, $konf = false) {
        $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => $id, ':param' => 'analytics')));
        if(!$select && !$konf) $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => 0, ':param' => 'analytics')));
        return $select;
    }

    public function getDevelopment($id = 0) {
        $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => $id, ':param' => 'development')));
        if(!$select && !$konf) $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => 0, ':param' => 'development')));
        return $select;
    }

    public function getRegulamin($id = 0) {
        $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => $id, ':param' => 'regulamin')));
        if(!$select && !$konf) $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => 0, ':param' => 'regulamin')));
        return $select;
    }

    public function getWstep($id = 0) {
        $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => $id, ':param' => 'wstep')));
        
        if(!$select && !$konf) $select = $this->fetchRow(
                                $this->select()
                                ->where('domena=:domain AND nazwa LIKE :param')
                                ->bind(array(':domain' => 0, ':param' => 'wstep')));
        return $select;
    }

    public function setAnalytics($id, $analytics) {
        $q = $this->fetchRow($this->select()
                                ->where('domena=:domena AND nazwa LIKE :param')
                                ->bind(array(':domena' => $id, ':param' => 'analytics')));
        if ($q) {
            $q->wartosc = $analytics;
            $q->save();
        } else {
            if(strlen($analytics)>0) $this->insert(array(
                'domena'    =>  $id,
                'nazwa'     =>  'analytics',
                'wartosc'   =>  $analytics
            ));
        }
    }

    public function setDevelopment($id, $development) {
        $q = $this->fetchRow($this->select()
                                ->where('domena=:domena AND nazwa LIKE :param')
                                ->bind(array(':domena' => $id, ':param' => 'development')));
        if ($q) {
            $q->wartosc = $development;
            $q->save();
        } else {
            $this->insert(array(
                'domena'    =>  $id,
                'nazwa'     =>  'development',
                'wartosc'   =>  $development
            ));
        }
    }

    public function setRegulamin($id, $regulamin) {
        $q = $this->fetchRow($this->select()
                                ->where('domena=:domena AND nazwa LIKE :param')
                                ->bind(array(':domena' => $id, ':param' => 'regulamin')));
        if ($q) {
            $q->wartosc = $regulamin;
            $q->save();
        } else {
            if(strlen($regulamin)>0) $this->insert(array(
                'domena'    =>  $id,
                'nazwa'     =>  'regulamin',
                'wartosc'   =>  $regulamin
            ));
        }
    }

    public function setWstep($id, $wstep) {
        $q = $this->fetchRow($this->select()
                                ->where('domena=:domena AND nazwa LIKE :param')
                                ->bind(array(':domena' => $id, ':param' => 'wstep')));
        if ($q) {
            $q->wartosc = $wstep;
            $q->save();
        } else {
            if(strlen($wstep)>0) $this->insert(array(
                'domena'    =>  $id,
                'nazwa'     =>  'wstep',
                'wartosc'   =>  $wstep
            ));
        }
    }

}
